<?php
namespace Net\Curl;

use \Base\UnitTest\RunkitTestCase as RunkitTestCase;

require_once __DIR__ . '/RunkitTestCase.php';
require_once __DIR__ . '/../CurlMulti.php';

class CurlMultiTest
    extends RunkitTestCase
{
    protected $mock;

    protected function setUp()
    {
        $this->mock = $this->runkitMockFunctions(array(
            'curl_multi_init',
            'curl_multi_close',
            'curl_multi_add_handle',
            'curl_multi_getcontent',
            'curl_multi_select',
            'curl_multi_remove_handle',
        ));
    }

    protected function tearDown()
    {
        $this->runkitRevertAll();
    }

    public function testExec()
    {
        $func = 'curl_multi_exec';
        $this->runkitOverride($func, '$h, &$code', 'assert($h == "my handle"); $code = 42; return 123;');

        $this->mock
            ->expects($this->at(0))
            ->method('curl_multi_init')
            ->with()
            ->will($this->returnValue('my handle'));

        $m = new CurlMulti();
        $this->assertEquals(123, $m->exec($flag));
        $this->assertEquals(42, $flag);

        $this->runkitRevert($func);
    }

    public function testInfoRead()
    {
        $func = 'curl_multi_info_read';
        $this->runkitOverride($func, '$h, &$code', 'assert($h == "my handle"); $code = 42; return 123;');

        $this->mock
            ->expects($this->at(0))
            ->method('curl_multi_init')
            ->with()
            ->will($this->returnValue('my handle'));

        $m = new CurlMulti();
        $this->assertEquals(123, $m->infoRead($flag));
        $this->assertEquals(42, $flag);

        $this->runkitRevert($func);
    }


    public function testAllOtherMethods()
    {
        $c = new Curl();

        $this->mock
            ->expects($this->at(0))
            ->method('curl_multi_init')
            ->with()
            ->will($this->returnValue('my handle'));

        $this->mock
            ->expects($this->at(1))
            ->method('curl_multi_add_handle')
            ->with('my handle', $c->getHandle())
            ->will($this->returnValue(42));

        $this->mock
            ->expects($this->at(2))
            ->method('curl_multi_getcontent')
            ->with($c->getHandle())
            ->will($this->returnValue('my content'));

        $this->mock
            ->expects($this->at(3))
            ->method('curl_multi_select')
            ->with('my handle', 1.0)
            ->will($this->returnValue('my select 1'));

        $this->mock
            ->expects($this->at(4))
            ->method('curl_multi_select')
            ->with('my handle', 12)
            ->will($this->returnValue('my select 2'));

        $this->mock
            ->expects($this->at(5))
            ->method('curl_multi_remove_handle')
            ->with('my handle', $c->getHandle())
            ->will($this->returnValue('my remove'));

        $this->mock
            ->expects($this->once())
            ->method('curl_multi_close')
            ->with('my handle');

        $m = new CurlMulti();
        $this->assertEquals(42, $m->add($c));
        $this->assertEquals('my content', $m->getContent($c));
        $this->assertEquals('my select 1', $m->select());
        $this->assertEquals('my select 2', $m->select(12));
        $this->assertEquals('my remove', $m->remove($c));
    }
}
